/**
 * 
 */
package com.xidian.zhiancloud.controller.file;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.model.exam.SysFile;
import com.xidian.zhiancloud.service.file.FileService;
import com.xidian.zhiancloud.util.tools.FileUtils;



/**
 * @author zwt
 *
 * 2021年3月9日
 */
@RestController
@RequestMapping("/file")
public class FileController {
	 private static final Logger log = LoggerFactory.getLogger(FileController.class);
	 	/**
	     * 上传文件存储在本地的根路径
	     */
	
	 @Autowired
	 private FileService fileService;
	@PostMapping("upload")
    public ResultDTO upload
            (@RequestParam("file")MultipartFile file,
             @RequestParam("folder")String folder){
        try{
           
            String url = fileService.uploadFile(file, folder);
            
            SysFile sysFile = new SysFile();
            sysFile.setName(FileUtils.getName(url));
            sysFile.setUrl(url);
            System.out.println(sysFile);
            return ResultDTO.successOf(sysFile, GeneralResponseEnums.SUCCESS);
        }
        catch (Exception e)
        {
            log.error("上传文件失败", e);
            return ResultDTO.errorOf(GeneralResponseEnums.UPLOAD_FAILED);
        }
    }
}
